<template>
  <div>
    <div style="text-align: center">
      <el-button type="success" size="mini" @click="getInfo(2,1)">已审核</el-button>
      <el-button type="danger" size="mini" @click="getInfo(1,1)">未审核</el-button>
    </div>
    <el-row style="margin-top: 20px">
      <el-table :data="commentInfos" border>
        <el-table-column label="ID" prop="id" align="center" width="50"></el-table-column>
        <el-table-column label="用户名" prop="name" align="center" width="100"></el-table-column>
        <el-table-column label="邮箱" prop="email" align="center" width="150"></el-table-column>
        <el-table-column label="评论内容" prop="content" align="center" width="400"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button size="mini" type="primary" v-if="scope.row.published===false" @click="updateComment(scope.$index,scope.row)">通过</el-button>
            <el-button size="mini" type="danger" @click="deleteComment(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "Comments",
  data(){
    return{
      commentInfos: [],
      deleteInfo:{}
    }
  },
  mounted(){
    this.getInfo(null,1);
  },
  methods: {
    getInfo(i,current){
      let method = 3;
      if ( i === 1){
        method = 1;
      }
      else if (i === 2){
        method = 2;
      }
      axios.get("/admin/getComment",{params:{method:method,current:current,pageSize:10}}).then( (res) => {
        this.commentInfos = res.data.records;
      })
    },
    updateComment(index,row){
      row.published=true;
      row.md5Password=localStorage.getItem("password");
      axios.post("/admin/updateComment",row).then( (res) => {
        if (res.data === true){
          this.$message.success("通过成功");
        }
        else
          this.$message.error("通过失败");
      })
    },
    deleteComment(index,row){
      row.md5Password=localStorage.getItem("password");
      axios.post("/admin/deleteComment",row).then( (res) => {
        if ( res.data === true){
          this.$message.success("删除成功");
          this.commentInfos.splice(index,1);
        }
        else
          this.$message.error("删除失败")
      })
    }
  }
}
</script>

<style scoped>

</style>